package com.java.androidmarket.service.impl;

import com.java.androidmarket.dao.StarDao;
import com.java.androidmarket.entity.Goods;
import com.java.androidmarket.service.StarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description:
 * author:张腾
 * date:2021-04-27
 */

@Service
public class StarServiceImpl implements StarService {

    @Autowired
    private StarDao starDao;

    @Override
    public List<Goods> getStarGoods(int userId) {
        List<Goods> starGoods = starDao.getStarGoods(userId);
        return starGoods;
    }

    @Override
    public int getStarCount(int userId) {
        int starCount = starDao.getStarCount(userId);
        return starCount;
    }

    /**
     * 根据用户ID与商品ID查询是否收藏，若否则为null被catch
     * @param userId
     * @param goodsId
     * @return
     */
    @Override
    public int ifStar(int userId, int goodsId) {
        int i = 0;
        try{
            i = starDao.ifStar(userId, goodsId);
        }catch (Exception e){
            //未收藏
            i = 0;
        }

        return i;
    }

    @Override
    public void addStar(int userId, List<Integer> goodsIds) {
        for (Integer goodsId : goodsIds) {
            if (ifStar(userId,goodsId)==0) {
                starDao.addStar(userId,goodsId);
            }
        }
    }

    @Override
    public void delStar(int userId, int goodsId) {
        if (ifStar(userId,goodsId)==1) {
            starDao.delStar(userId, goodsId);
        }
    }
}
